package org.example;

import org.example.entity.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;

/**
 * <p> 测试对 POJO 编解码 </p>
 *
 * @author Chris
 * @since 2024/3/25 22:59
 */
public class PerformTestUserInfo {
    /**
     * 使用 slf4j 日志框架
     */
    private final static Logger log = LoggerFactory.getLogger(PerformTestUserInfo.class);

    public static void main(String[] args) throws IOException {
        final UserInfo info = new UserInfo();
        info.buildUserID(100).buildUserName("Welcome to Netty");

        int loop = 1000000;
        ByteArrayOutputStream bos = null;
        ObjectOutputStream os = null;
        long startTime = System.currentTimeMillis();

        for (int i = 0; i < loop; i++) {
            bos = new ByteArrayOutputStream();
            os = new ObjectOutputStream(bos);
            os.writeObject(info);
            os.flush();
            os.close();
            final byte[] b = bos.toByteArray();
            bos.close();
        }

        long endTime = System.currentTimeMillis();
        log.info("The jdk serializable cost time is : {}ms", endTime - startTime);
        log.info("---------------------------------------------");

        final ByteBuffer buffer = ByteBuffer.allocate(1024);
        startTime = System.currentTimeMillis();
        for (int i = 0; i < loop; i++) {
            final byte[] b = info.codeC(buffer);
        }
        endTime = System.currentTimeMillis();
        log.info("The jdk serializable cost time is : {}ms", endTime - startTime);
    }

}